<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\widgets\LinkPager;
use yii\helpers\Url;

$this->params['breadcrumbs'][] = $this->title;

?>
  <div class="mcCont mcgCont">

      <div class="right myCollect">
        <h1 class="tt">我收藏的货品</h1>
        <form action="">
          <input type="text" placeholder="<?=Yii::$app->request->get('goodsname')?Yii::$app->request->get('goodsname'):'请输入货品的名称'?>" id="goodsname">
          <input type="submit" id="submit">
          <?php if (Yii::$app->request->get('goodsname')): ?>
            <input type="submit" value="清空查询">
          <?php endif ?>
        </form>
        <p class="listTt">
          <span class="check-box"></span>
          <span class="mr20">全选</span>
          <a style="cursor:pointer" id="addall">加入进货单</a>
          <a style="cursor:pointer" id="cancelall">取消收藏</a>
        </p>
        <ul class="lists">

          <?php foreach ($list as $key => $value): ?>
            <li>
              <a href="<?= Url::toRoute(['product/', 'id' => $value['id']]);?>">
                <img src="<?=Html::encode($value['img_url'])?>!s50x50" alt="" class="imge">
              </a>
              <span class="check-box" value='<?=Html::encode($value['id'])?>' id = "<?=Html::encode($value['id'])?>"></span>
              <p class="name"><?=Html::encode($value['name'])?></p>
              <span class="price">￥<?=Html::encode($value['product_price'])?>/<?=Html::encode($value['price_unit'])?></span>
              <span class="btns">
                <a href="javasctipt:void(0)" value='<?=Html::encode($value['id'])?>'  class="btn-small-empty btn">加入订货单</a>
                <a style="cursor:pointer" class="cancel" value = "<?=Html::encode($value['cid'])?>">取消收藏</a>
              </span>
            </li>
          <?php endforeach ?>
          
        </ul>
<?= LinkPager::widget([
      'pagination' => $pages,
      'maxButtonCount' => 5,
      'options' => ['class' => 'pages'], 
      'nextPageLabel' => '<span class="pageItem pageRight"><i class="iconfont icon-right"></i></span>', 
      'prevPageLabel' => '<span class="pageItem pageLeft"><i class="iconfont icon-left"></i></span>', 
]); ?>
      </div>
    </div>
  </div>
</div>
<style type="text/css">
  /*分页CSS修改*/
  .pages{margin-top: 17px;}
  ul.pages li a{width: 45px;display: inline-block;}
  ul.pages li{
    border: 1px solid #e5e5e5;
    border-radius: 4px;
    cursor: pointer;
    display: inline-block;
    font-size: 12px;
    height: 36px;
    width:45px;
    line-height: 36px;
    margin: 0 5px;
    text-align: center;
    vertical-align: middle;
  }
  .pages .next i{color:#333}
  .pages .prev i{color:#333}
  .pages .disabled .iconfont {color: #c3c3c3}
  .active{background:#31a900 none repeat scroll 0 0;}
  .active a{color: #fff;}
  .pages .prev{border:0;}
  .pages .next{border:0;}
</style>
<script type="text/javascript">
  $(function(){
    //查询收藏的物品
    $('#submit').on('click',function(){
      var goodsname = $('#goodsname').val();
      if(goodsname){
        window.location.href = "/account/collect/?goodsname="+goodsname;
      }else{
        layer.msg('请输入物品名称进行查询',{time:1200});
      }      
      return false;
    });

    //单选选中
    $('.lists li .check-box').on('click',function(){
      if($(this).attr('class') == "check-box"){
        $(this).addClass('selected');
      }else{
        $(this).removeClass('selected');
      }
    })

    //全选按钮
    $('.listTt .check-box').on('click',function(){
      if($(this).attr('class') == "check-box"){
        $(this).addClass('selected');
        $('.lists li .check-box').addClass('selected');
      }else{
        $(this).removeClass('selected');
        $('.lists li .check-box').removeClass('selected');
      }
    });

    //单个取消收藏
    $('.lists .cancel').on('click',function(){
      var pid = $(this).attr('value');

      layer.confirm('您确认要取消收藏该商品吗？', {
          btn: ['确认','取消'], //按钮
          shade: false, //不显示遮罩
          icon:3,   //5
      }, function(){
          $.post('',{'pid':pid},function(data){
          if(data>0){
            layer.msg('取消成功！', {time: 1000}, function(){
              window.location.href = "";
            });
          }
        });
      });
    });
    //批量删除收藏
    $('#cancelall').on('click',function(){
      var _list = [];
      var _cole = $('.lists li span[class = "check-box selected"]');
      if(_cole.attr('id')== undefined){
        layer.msg('你没有选择要取消的收藏哦~', {time: 1500});
      }else{
        layer.confirm('您确认要取消收藏选中的商品吗？', {
            btn: ['确认','取消'], //按钮
            shade: false, //不显示遮罩
            icon:3,   //5
        }, function(){
          for (var i = 0; i < _cole.length; i++) {  
            _list[i] = _cole[i].id;  
          }
          $.post('',{'pid':_list},function(data){
            if(data>0){
              layer.msg('取消成功！', {time: 1000}, function(){
                window.location.href = "";
              });
            }
          });
        });
      }
    });

    //批量加入购物车
    $('#addall').on('click',function(){
      var _list = [];
      var _cole = $('.lists li span[class = "check-box selected"]');
      if(_cole.attr('id')== undefined){
        layer.msg('请选择要加入购物车的收藏哦~', {time: 1500});
      }else{
        layer.confirm('确认将选中的商品加入购物车？', {
            btn: ['确认','取消'], //按钮
            shade: false, //不显示遮罩
            icon:3,   //5
        }, function(){
          for (var i = 0; i < _cole.length; i++) {  
            _list += '-'+_cole[i].id;     //-----所有选中的商品的id数据集合
          }
          $.ajax({
            type: "POST",
            url: "/shopping-cart/add-all",
            data: "product_id="+_list+"&product_num=1&_csrf=<?=Yii::$app->request->getCsrfToken()?>",
            dataType: "json",
            success: function(data){
              if(!data.errNo){
                $('#now_cart').html(data.nowProduct);
                layer.msg('加入购物车成功',{time:1200},function(){
                  /*$.post('',{'pid':pid},function(data){
                      if(data>0){*/
                        window.location.href = '';
                      /*}
                  })*/
                });
              }else{
                layer.msg(data.msg,{time:1200});
              }
            }
          });

        });
      }
    });

    //加入购物车
    $('.btns .btn').on('click',function(e){
      
      e.preventDefault();
      var product_num = 1;
      var product_id = $(this).attr('value');
      var pid = $(this).siblings('a').attr('value');
      
      var uid = <?php echo Yii::$app->user->isGuest?1:0?>;
      if(uid){
        layer.msg('请先登录',{time:1200},function(){
          window.location.href = '/site/login';
        });
        return false;
      }
      $.ajax({
        type: "POST",
        url: "/shopping-cart/add",
        data: "product_id="+product_id+"&product_num="+product_num+"&_csrf=<?=Yii::$app->request->getCsrfToken()?>",
        dataType: "json",
        success: function(data){
          if(!data.errNo){
            $('#now_cart').html(data.nowProduct);
            layer.msg('加入购物车成功',{time:1200},function(){
              /*$.post('',{'pid':pid},function(data){
                  if(data>0){*/
                    window.location.href = '';
                  /*}
              })*/
            });
          }else{
            layer.msg(data.msg,{time:1200});
          }
        }
      });
    })
  });
</script>